package cc.woori.mobile;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.woori.mobile.bean.NoteEntity;

import com.google.appengine.api.datastore.Text;

public class RegisterNoteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	    throws ServletException, IOException {

	String title = req.getParameter("title");

	Text body = new Text(req.getParameter("body").replaceAll("\\n",
		"<br />"));

	NoteEntity entry;
	PersistenceManager pm = PMF.get().getPersistenceManager();

	try {
	    entry = new NoteEntity(title, body);

	    pm.makePersistent(entry);
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} finally {
	    if (pm != null) {
		pm.close();
	    }

	    RequestDispatcher view = req
		    .getRequestDispatcher("/admin/adminNotice.jsp");
	    view.forward(req, resp);
	}

    }
}
